{extend name="base"/}
{block name="resources"}
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
    <div class="layui-colla-item">
        <h2 class="layui-colla-title">操作提示</h2>
        <ul class="layui-colla-content layui-show">
            <li>您可以在此添加酒店的房间</li>
        </ul>
    </div>
</div>

<div class="ns-single-filter-box">
    <button class="layui-btn ns-bg-color" onclick="add()">添加房间</button>
</div>
<div class="ns-screen layui-collapse" lay-filter="selection_panel">
    <div class="layui-colla-item">
        <h2 class="layui-colla-title">筛选</h2>

        <form class="layui-colla-content layui-form layui-show">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">房间名称：</label>
                    <div class="layui-input-inline">
                        <input type="text" id="search_text" name="search_text" placeholder="请输入房间名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">房间类型：</label>
                    <div class="layui-input-inline">
                        <select name="type_id" lay-filter="type_id">
                            <option value="">全部</option>
                            {volist name="$type_list" id="group"}
                            <option value="{$group.baoyu_type_id}">{$group.type_name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

            </div>

            <div class="ns-form-row">
                <button class="layui-btn ns-bg-color" lay-submit lay-filter="search">筛选</button>

                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </form>

    </div>
</div>

<!--<div class="ns-single-filter-box">
    <button class="layui-btn ns-bg-color" onclick="add()">添加房间</button>

    <div class="layui-form">
        <div class="layui-input-inline ns-len-mid">
            <input type="text" name="search_text" placeholder="请输入房间名称" class="layui-input">
            <button type="button" class="layui-btn layui-btn-primary" lay-filter="search" lay-submit>
                <i class="layui-icon">&#xe615;</i>
            </button>
        </div>
    </div>

</div>-->

<table id="adv_list" lay-filter="adv_list"></table>

<!-- 广告图片 -->
<script type="text/html" id="adv_image">
    <div class="ns-img-box">
        <div class="ns-img-box img_block_sty">
            {{#  if(d.images){  }}
            <img layer-src src="{{ns.img(d.images.split(',')[0])}}" class="img_sty"/>
            {{#  }else{  }}
            <img layer-src src="{:img($default_img.default_city_img)}" class="img_sty"/>
            {{#  }  }}
        </div>
    </div>
</script>

<!-- 排序 -->
<script type="text/html" id="sort">
    <input name="sort" type="number" onchange="editSort({{d.room_id}},'sort', this)" value="{{d.sort}}" class="layui-input ns-sort-len">
</script>

<!-- 操作 -->
<script type="text/html" id="action">
    <div class="ns-table-btn">
        <a class="layui-btn" lay-event="edit">编辑</a>
        <a class="layui-btn" lay-event="delete">删除</a>
        {{#  if(d.status == 1){  }}
        <a class="layui-btn" lay-event="removed">下架</a>
        {{#  }  }}
        {{#  if(d.status == 0){  }}
        <a class="layui-btn" lay-event="listed">上架</a>
        {{#  }  }}
    </div>
</script>


{/block}
{block name="script"}
<script>
    layui.use('form', function () {
        var table,
            form = layui.form,
            repeat_flag = false; //防重复标识
        form.render();

        table = new Table({
            elem: '#adv_list',
            url: ns.url("hotel://hotel/room/lists"),
            cols: [
                [ {
                    title: '房间图片',
                    unresize: 'false',
                    templet: '#adv_image',
                    width: '15%'
                },  {
                    field: 'room_name',
                    title: '房间名称',
                    unresize: 'false',
                    width: '15%'
                }, {
                    field: 'type_name',
                    title: '房间类型',
                    unresize: 'false',
                    width: '15%'
                },{
                    field: 'price',
                    title: '价格',
                    unresize: 'false',
                    width: '8%'
                },{
                    title: '状态',
                    unresize: 'false',
                    width: '5%',
                    templet: function (data) {
                        return data.status == 1 ? '上架' : '下架';
                    }
                }, {
                    title: '排序',
                    unresize: 'false',
                    templet: '#sort',
                    width: '12%'
                }, {
                    title: '创建时间',
                    unresize: 'false',
                    width: '12%',
                    templet: function (res) {
                        if (res.create_time == 0) {
                            return '--';
                        } else {
                            return ns.time_to_date(res.create_time)
                        }
                    }
                },  {
                    title: '操作',
                    toolbar: '#action',
                    unresize: 'false',
                    width: '12%'
                }]
            ],

        });

        /**
         * 监听工具栏操作
         */
        table.tool(function(obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit': //编辑
                    location.href = ns.url("hotel://hotel/room/editroom",{"room_id":data.room_id});
                    break;
                case 'delete': //删除
                    deleteRoom(data.room_id);
                    break;
                case 'removed': //下架
                    removedRoom(data.room_id);
                    break;
                case 'listed': //上架
                    listedRoom(data.room_id);
                    break;
            }
        });

        /**
         * 批量操作
         */
        table.bottomToolbar(function(obj) {
            if (obj.data.length < 1) {
                layer.msg('请选择要操作的数据');
                return;
            }

            switch (obj.event) {
                case "del":
                    var id_array = new Array();
                    for (i in obj.data) id_array.push(obj.data[i].room_id);
                    deleteRoom(id_array.toString());
                    break;
            }
        });

        //下架
        function removedRoom(room_id) {
            $.ajax({
                url: ns.url("hotel://hotel/room/operationRoom"),
                data: {'room_id': room_id,'status':0},
                dataType: 'JSON',
                type: 'POST',
                success: function(res) {
                    layer.msg(res.message);

                    if (res.code == 0) {
                        table.reload();
                    }
                }
            });
        }

        //上架
        function listedRoom(room_id) {
            $.ajax({
                url: ns.url("hotel://hotel/room/operationRoom"),
                data: {'room_id': room_id,'status':1},
                dataType: 'JSON',
                type: 'POST',
                success: function(res) {
                    layer.msg(res.message);

                    if (res.code == 0) {
                        table.reload();
                    }
                }
            });
        }

        /**
         * 删除
         */
        function deleteRoom(room_ids) {
            if (repeat_flag) return false;
            repeat_flag = true;

            layer.confirm('确定要删除该房间吗?', function() {
                $.ajax({
                    url: ns.url("hotel://hotel/room/deleteRoom"),
                    data: {'room_ids': room_ids},
                    dataType: 'JSON',
                    type: 'POST',
                    success: function(res) {
                        layer.msg(res.message);
                        repeat_flag = false;

                        if (res.code == 0) {
                            table.reload();
                        }
                    }
                });
            }, function () {
                layer.close();
                repeat_flag = false;
            });
        }

        /**
         * 监听搜索
         */

        form.on('submit(search)', function(data) {
            table.reload({
                page: {
                    curr: 1
                },
                where: data.field
            });
            return false;
        });
    });

    // 监听单元格编辑
    function editSort(room_id, type, event){
        var value = $(event).val();

        if(!new RegExp("^-?[1-9]\\d*$").test(value)){
            layer.msg("排序号只能是整数");
            return ;
        }
        if(value<0){
            layer.msg("排序号必须大于0");
            return ;
        }

        $.ajax({
            type: 'POST',
            url: ns.url("hotel://hotel/room/editRoomField"),
            data: {'type': type, 'value': value, 'room_id': room_id},
            dataType: 'JSON',
            success: function(res) {
                layer.msg(res.message);
                if(res.code==0){
                    table.reload();
                }
            }
        });
    }

    function add() {
        location.href = ns.url("hotel://hotel/room/addRoom");
    }
</script>
{/block}